//@author A0081189X
package net.mysoc.w111j.service.actions;

import net.mysoc.w111j.model.BadPaymentTypeNameException;
import net.mysoc.w111j.model.PaymentType;
import net.mysoc.w111j.model.User;

/**
 * An {@code Action} to undo the renaming of a {@code PaymentType}.
 */
public class RenamePaymentTypeAction implements Action {
    private final User user;
    private final PaymentType oldPaymentType;
    private String status;

    public RenamePaymentTypeAction(User user, PaymentType oldPaymentType) {
        this.user = user;
        this.oldPaymentType = new PaymentType(oldPaymentType.getId(), oldPaymentType.getName());
        setStatus(oldPaymentType);
    }

    @Override
    public void undo() {
        try {
            user.updatePaymentType(oldPaymentType);
        } catch (BadPaymentTypeNameException e) {
            // This is guaranteed to not happen here.
            throw new RuntimeException(e);
        }
    }

    private void setStatus(PaymentType paymentType) {
        status = "Renamed payment type: " + paymentType.getName();
    }

    @Override
    public String getStatus() {
        return status;
    }
}
